import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class PacManMaze extends JComponent implements KeyListener  {

	
	int dir = 0;
	int xPosition = 9;
	int yPosition = 8;
	PacMan pacman = new PacMan();
	char map [][] = {
			{'w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w'},
			{'w','b','b','b','b','b','b','b','b','w','b','b','b','b','b','b','b','b','w'},
			{'w','b','w','w','b','w','w','w','b','w','b','w','w','w','b','w','w','b','w'},
			{'w','b','w','w','b','w','w','w','b','w','b','w','w','w','b','w','w','b','w'},
			{'w','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','w'},
			{'w','b','w','w','b','w','b','w','w','w','w','w','b','w','b','w','w','b','w'},
			{'w','b','b','b','b','w','b','b','b','w','b','b','b','w','b','b','b','b','w'},
			{'w','w','w','w','b','w','w','w','b','w','b','w','w','w','b','w','w','w','w'},
			{'w','w','w','w','b','w','b','b','b','b','b','b','b','w','b','w','w','w','w'},
			{'w','w','w','w','b','w','b','w','w','w','w','w','b','w','b','w','w','w','w'},
			{'b','b','b','b','b','b','b','w','b','b','b','w','b','b','b','b','b','b','b'},
			{'w','w','w','w','b','w','b','w','w','w','w','w','b','w','b','w','w','w','w'},
			{'w','w','w','w','b','w','b','b','b','b','b','b','b','w','b','w','w','w','w'},
			{'w','w','w','w','b','w','b','w','w','w','w','w','b','w','b','w','w','w','w'},
			{'w','b','b','b','b','b','b','b','b','w','b','b','b','b','b','b','b','b','w'},
			{'w','b','w','w','b','w','w','w','b','w','b','w','w','w','b','w','w','b','w'},
			{'w','b','b','w','b','b','b','b','b','b','b','b','b','b','b','w','b','b','w'},
			{'w','w','b','w','b','w','b','w','w','w','w','w','b','w','b','w','b','w','w'},
			{'w','b','b','b','b','w','b','b','b','w','b','b','b','w','b','b','b','b','w'},
			{'w','b','w','w','w','w','w','w','b','w','b','w','w','w','w','w','w','b','w'},
			{'w','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','w'},
			{'w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w'}
	
	
	};
	
	
	
	
	
	
	public PacManMaze() {
		super();
		this.addKeyListener(this);
		this.setVisible(true);
		this.setFocusable(true);
		Dimension dim  = new Dimension(500,500);
		this.setPreferredSize(dim);
		this.setFocusable(true);	
		
		
		
	}
	
	public void paintComponent (Graphics g){
		super.paintComponent(g);
		paintBoard(g);
		
		
		
	}
	
	public void paintPacman (Graphics2D g,int xPosition, int yPosition){
		
		pacman.paintPacMan(g,xPosition,yPosition);
	}
	
	public void paintBoard (Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		g2.scale(500/19,500/22);
		g2.setStroke(new BasicStroke (1.0F/500));
		
		for(int row=0;row<map.length;row++){
			for(int column=0;column<map[row].length;column++){
				if(map[row][column] == 'w'){
					g2.setColor(Color.RED);
					g2.fillRect(column, row, 1, 1);
					
				}
				else if(map[row][column] == 'b'){
					g2.setColor(Color.BLACK);
					g2.fillRect(column, row, 1, 1);
					
				}
			}
		}
		
		paintPacman(g2,xPosition,yPosition);
		
	}
	
	public int getXPosition(){
		return xPosition;
	}
	public int getYPosition(){
		return yPosition;
	}
	
	
	

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyChar() == 'w'){
			dir = 1;
			int positionY = getYPosition();
			int positionX = getXPosition();
			
//			if(map[positionY-1][positionX] == 'b'){
//					yPosition--;
//					repaint();
//			}
			for(int counter=0;counter<22;counter++){
				positionY = getYPosition();
				if(map[positionY-1][positionX] == 'b'){
					
					yPosition--;
					repaint();
				}
				if(e.getKeyChar() == 'd' || e.getKeyChar() == 's' || e.getKeyChar() == 'a'){
					break;
				}
				try {
					Thread.sleep(400);
				} catch (InterruptedException e1) {
					
					e1.printStackTrace();
				}
			}
				
		}
		
		
		else if(e.getKeyChar() == 'd'){
			dir = 2;
			int positionY = getYPosition();
			int positionX = getXPosition();
			if(map[positionY][positionX+1] == 'b'){
					xPosition++;
					repaint();
			}
			
		}
		
		
		else if(e.getKeyChar() == 's'){
			dir = 3;
			int positionY = getYPosition();
			int positionX = getXPosition();
			if(map[positionY+1][positionX] == 'b'){
					yPosition++;
					repaint();
			}
			
		}
		
		
		else if(e.getKeyChar() == 'a'){
			dir = 4;
			int positionY = getYPosition();
			int positionX = getXPosition();
			if(map[positionY][positionX-1] == 'b'){
					xPosition--;
					repaint();
			}	
			
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
		
	}

	@Override
	public void keyTyped(KeyEvent event) {
	
		
	}
}
